//
//  HSqlManager.m
//  sqliteToEnity
//
//  Created by hament on 13-7-29.
//  Copyright (c) 2013年 hament. All rights reserved.
//

#import "HSqlManager.h"
#import "HCoreConfig.h"
#import "FMDatabase.h"
#import "FMDatabaseQueue.h"
#import "TableEnity.h"
#import "AttributeEnity.h"


@implementation HSqlManager

static HSqlManager * sqlManager=nil;


+(HSqlManager*) shareSqlManager{
    
    if (sqlManager==nil) {
        sqlManager=[[HSqlManager alloc] init];
    }

    return sqlManager;
}



//检查数据库是否能打开
+(BOOL)checksqliteDBWithPath:(NSString*)path{

    BOOL openSuccess=NO;
    
    NSString * dbPath=[path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL * url=[NSURL URLWithString:dbPath];
    FMDatabase * db = [FMDatabase databaseWithPath:[url path]]; //使用url和避免中文字符,空格等无法读取
    if ([db open]) {
        openSuccess=YES;
        [db close];
    }
    return openSuccess;
}


+(NSMutableArray*)getAllEnitysWithPah:(NSString*)apath{
    NSMutableArray * arr=[NSMutableArray array];
    NSString * dbPath=[apath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    
    NSURL * url=[NSURL URLWithString:dbPath];
    
    FMDatabase * db = [FMDatabase databaseWithPath:[url path]]; //使用url和避免中文字符,空格等无法读取
    if ([db open]) {
         NSString * sql=@"SELECT * FROM  sqlite_master WHERE type='table'";//获取table列表
        FMResultSet * rs = [db executeQuery:sql];
        while ([rs next]) {
            NSString *  enityName=[rs stringForColumn:@"name"];//实体类名字
            
            if ([enityName isEqualToString:@"sqlite_stat1"] ||[enityName isEqualToString:@"sqlite_sequence"]) {
                continue;
            }
            
            TableEnity * te=[[TableEnity alloc] init];
            te.enityName=enityName;
            te.isSelected=YES;
            
            NSMutableArray * attriArr=[NSMutableArray array];
            
             NSString * arrsql=[NSString stringWithFormat:@"pragma table_info(%@)",enityName];
            
            //属性
             FMResultSet * ars = [db executeQuery:arrsql];
            while ([ars next]) {
            
                 NSString *  theenityName=[ars stringForColumn:@"name"];//属性名字
                 NSString *  datatype=CHECKSTR([ars stringForColumn:@"type"]);//sqlType
                
                NSString * covertToType=[HCoreConfig covertToAttriType:datatype];//转换
                
                AttributeEnity * arrEnity=[[AttributeEnity alloc] init];
                arrEnity.attributeName=CHECKSTR(theenityName) ;
                arrEnity.sqliteType=datatype;
                arrEnity.sqliteAttributeAndType=[arrEnity.attributeName stringByAppendingFormat:@":%@",arrEnity.sqliteType];
                arrEnity.objcType=covertToType;
                arrEnity.isSelected=YES;
                [attriArr addObject:arrEnity];
            }
            te.attributes=(NSArray*)attriArr;
            
            [arr addObject:te];
        }
        [db close];
    }
    
    
    return arr;
}



+(NSArray*)getDataWithKeys:(NSArray*)keysArr tableName:(NSString*)tableName dbPath:(NSString*)path{

    
    
    NSString * dbPath=[path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL * url=[NSURL URLWithString:dbPath];
    
    NSMutableArray * arr=[NSMutableArray array];
    NSString * updateKeys=@"";
    for (NSString * key in keysArr) {
        updateKeys=[updateKeys stringByAppendingFormat:@"\"%@\",",key];
        
    }
    
    //如果有key
    if ([updateKeys length]>1) {
        updateKeys=[updateKeys substringToIndex:[updateKeys length]-1];
    }
    
    NSString * sql=[NSString stringWithFormat:@"select %@ from %@ ",updateKeys,tableName];
    //debugLog(@"sql:%@",sql);
    FMDatabase * db = [FMDatabase databaseWithPath:[url path]];
 
    if ([db open]) {
        FMResultSet * rs = [db executeQuery:sql];
        while ([rs next]) {
            NSMutableDictionary * dic=[NSMutableDictionary dictionary];
            for (NSString * key in keysArr) {
                NSString * propertyValue =CHECKSTR([rs stringForColumn:key]);
                [dic setObject:propertyValue forKey:key];
            }
            
            [arr addObject:dic];
        }

        [db close];
    }
     
    return (NSArray*)arr;
}



@end
